/*
 * This file is part of the programmer editor demo
 * Copyright (C) 2005 Stephen Ostermiller
 * http://ostermiller.org/contact.pl?regarding=Syntax+Highlighting
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * See COPYING.TXT for details.
 */
package org.opu.stylepane.Ostermiller.Syntax;

import java.awt.Color;
import java.awt.Font;
import java.util.HashMap;

import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

class TokenStyles {
    /** default font size = 12 */
    public static final int DEFAULT_FONT_SIZE = 12;
    /** default font name = "monospaced" */
    public static final String DEFAULT_FONT_NAME = Font.MONOSPACED;
    
    /** body */
    public static final String BODY = "body";
    /** comment */
    public static final String COMMENT = "comment";
    /** endtag */
    public static final String ENDTAG = "endtag";
    /** error */
    public static final String ERROR = "error";
    /** grayedOut */
    public static final String GRAYEDOUT = "grayedOut";
    /** identifier */
    public static final String IDENTIFIER = "identifier";
    /** literal */
    public static final String LITERAL = "literal";
    /** name */
    public static final String NAME = "name";
    /** operator */
    public static final String OPERATOR = "operator";
    /** preprocessor */
    public static final String PREPROCESSOR = "preprocessor";
    /** reference */
    public static final String REFERENCE = "reference";
    /** reservedWord */
    public static final String RESERVEDWORD = "reservedWord";
    /** separator */
    public static final String SEPARATOR = "separator";
    /** tag */
    public static final String TAG = "tag";
    /** text */
    public static final String TEXT = "text";
    /** unknown */
    public static final String UNKNOWN = "unknown";
    /** value */
    public static final String VALUE = "value";
    /** whitespace */
    public static final String WHITESPACE = "whitespace";
    
	private TokenStyles() { } // disable constructor
	
	/**
	 * A hash table containing the text styles. Simple attribute sets are hashed
	 * by name (String)
	 */
	private static HashMap styles = new HashMap();

	/**
	 * Create the styles and place them in the hash table.
	 */
	static {
		Color maroon = new Color(0xB03060);
		Color darkBlue = new Color(0x000080);
		Color darkGreen = Color.GREEN.darker();
		Color darkPurple = new Color(0xA020F0).darker();

		addStyle(BODY,         Color.WHITE, Color.BLACK,  false,  false);
		addStyle(TAG,          Color.WHITE, Color.BLUE,   true,   false);
		addStyle(ENDTAG,       Color.WHITE, Color.BLUE,   false,  false);
		addStyle(REFERENCE,    Color.WHITE, Color.BLACK,  false,  false);
		addStyle(NAME,         Color.WHITE, maroon,       true,   false);
		addStyle(VALUE,        Color.WHITE, maroon,       false,  true);
		addStyle(TEXT,         Color.WHITE, Color.BLACK,  false,  false);
		addStyle(RESERVEDWORD, Color.WHITE, Color.BLUE,   false,  false);
		addStyle(IDENTIFIER,   Color.WHITE, Color.BLACK,  false,  false);
		addStyle(LITERAL,      Color.WHITE, maroon,       false,  false);
		addStyle(SEPARATOR,    Color.WHITE, darkBlue,     false,  false);
		addStyle(OPERATOR,     Color.WHITE, Color.BLACK,  true,   false);
		addStyle(COMMENT,      Color.WHITE, darkGreen,    false,  false);
		addStyle(PREPROCESSOR, Color.WHITE, darkPurple,   false,  false);
		addStyle(WHITESPACE,   Color.WHITE, Color.BLACK,  false,  false);
		addStyle(ERROR,        Color.WHITE, Color.RED,    false,  false);
		addStyle(UNKNOWN,      Color.WHITE, Color.ORANGE, false,  false);
		addStyle(GRAYEDOUT,    Color.WHITE, Color.GRAY,   false,  false);
	}
	
	private static void addStyle(String name, Color bg, Color fg, boolean bold,
			boolean italic) {
		SimpleAttributeSet style = new SimpleAttributeSet();
		StyleConstants.setFontFamily(style, DEFAULT_FONT_NAME);
		StyleConstants.setFontSize(style,DEFAULT_FONT_SIZE);
		StyleConstants.setBackground(style, bg);
		StyleConstants.setForeground(style, fg);
		StyleConstants.setBold(style, bold);
		StyleConstants.setItalic(style, italic);
		styles.put(name, style);
	}

	/**
	 * Retrieve the style for the given type of token.
	 * 
	 * @param styleName
	 *            the label for the type of text ("tag" for example) or null if
	 *            the styleName is not known.
	 * @return the style
	 */
	public static AttributeSet getStyle(String styleName) {
		return (AttributeSet) styles.get(styleName);
	}
}
